<template>
  <div class="index">
    <div v-for="i in indexList" :key="i.id" class="item" @click="getMore(i.id)">
      <div><img :src="i.titleImage" alt="" /></div>
      <div class="div">
        <h2>{{ i.title }}</h2>
        <p>{{ i.description }}</p>
        <span>{{ i.tags[0].name }}</span>
      </div>
    </div>
    <div class="pagination">
      <!-- 分页器 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="count"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { index } from "../../../units/api"; //导入使用的接口
export default {
  data() {
    return {
      pageSize: 10, //这个是传入值 界面显示多少条
      page: 1, //当前页
      // currentPage1:1,
      indexList: [], //用于接收渲染的数组
      count: 0,
    };
  },
  created() {
    this.getListAPI();
  },
  methods: {
    async getListAPI() {
      let res = await index({ current: this.page, size: this.pageSize });
      // console.log(res, "这里是index");
      this.indexList = structuredClone(res.data.data);
      this.count = res.data.total;
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getListAPI();
    },
    getMore(id) {
      this.$router.push({
        name: "indexmore",
        params: {
          id,
        },
      });
      console.log(id, "传入数据");
    },
  },
};
</script>

<style scoped lang="scss">
* {
  padding: 0;
  margin: 0;
}
.index {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 20px;
  .item {
    width: 48.5%;
    height: 400px;
    background-color: #fff;
    margin-bottom: 40px;
    div {
      width: 100%;
      height: 60%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .div {
      text-align: left;
      box-sizing: border-box;
      padding: 20px;
      line-height: 42px;
      h2 {
        font-size: 26px;
      }
      p {
        color: #666;
      }
      span {
        background-color: greenyellow;
        padding: 5px;
        border-radius: 10px;
      }
    }
  }
}
.pagination {
  margin: 0 auto;
}
</style>